/**
 * @param {string[]} chessboard
 * @return {number}
 */
 var flipChess = function(chessboard) {
  chessboard = chessboard.map(item => item.split(''))
  const m = chessboard.length, n = chessboard[0].length
  const dir = [[0,1], [1,0], [0,-1], [-1,0], [1,1], [1,-1], [-1,1], [-1,-1]]
  const dfs = (i, j, cnt) => {
    if(i >= 0 && i < m && j >= 0 && j < n && chessboard[i][j] !== '.'){
      if(chessboard[i][j] === 'X' && cnt > 0){
        return cnt
      }
      if(chessboard[i][j] === 'O'){
        cnt++
      }
      const before = chessboard[i][j]
      chessboard[i][j] = 'X'
      let sum = 0
      for(const [dx, dy] of dir){
        sum += dfs(i+dx, i+dy, cnt)
      }
      chessboard[i][j] = before
      return sum
    }
    return 0
  }
  let ans = 0
  for(let i = 0; i<m; i++){
    for(let j = 0; j<n; j++){
      if(chessboard[i][j] === '.'){
        chessboard[i][j] = 'X'
        ans = Math.max(ans, dfs(i, j,0))
        chessboard[i][j] = '.'
      }
    }
  }
  return ans
};